package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.dto.DishDto;
import com.itheima.service.DishService;
import com.itheima.vo.DishVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//跨域请求的一种方式 不行
// @CrossOrigin
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @PostMapping
    public CommonResult save(@RequestBody DishDto dishDto){
        // System.out.println(dish);
        boolean adddish = dishService.save(dishDto);
        if (!adddish){
            return CommonResult.error("添加失败");
        }
        return CommonResult.success("新增菜品成功");
    }
    @GetMapping("list")
    public CommonResult getList( String categoryId){
        List<DishVo> dishVos = dishService.getList(categoryId);
        return CommonResult.success(dishVos);
    }


    //@RequestParam("page") 默认是有的  请求参数  defaultValue = "1"  默认值是1
    @GetMapping("/page")
    public CommonResult page( String name,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Integer type){
        PageInfo<DishVo> pageInfo = dishService.page(name,page, pageSize,type);
        return CommonResult.success(pageInfo);
    }
    //修改菜单
   @PutMapping
    public CommonResult update(@RequestBody DishDto dishDto){
       boolean upd = dishService.update(dishDto);
       if (!upd){
           return CommonResult.error("修改失败");
       }
       return CommonResult.success("修改菜品成功");
   }
   @DeleteMapping
    public CommonResult delete(@RequestParam("ids") List<String> ids){
            log.info("删除的id是：",ids);
           boolean delete = dishService.delete(ids);

           if (!delete){
               return CommonResult.error("菜品不存在");
           }

       return CommonResult.success("删除成功");
   }
   @GetMapping("/{id}")
    public CommonResult getById(@PathVariable String id){
       DishVo dishVO = dishService.getById(id);
        if (dishVO==null){
            return CommonResult.error("菜品不存在");
        }
       return CommonResult.success(dishVO);
   }
   @PostMapping("/status/{status}")
   public CommonResult  sell(@RequestParam("ids") List<String> ids,@PathVariable int status){
       boolean sell = dishService.sell(ids,status);
       if (!sell){
           return CommonResult.error("操作失败");
       }
        return  CommonResult.success("操作成功");
   }
}
